package com.example.service.impl;

import com.example.entity.Goods;
import com.example.mapper.GoodsMapper;
import com.example.service.GoodsService;
import com.example.utils.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Integer warehouseId) {

        PageHelper.startPage(page, pageSize);

        List<Goods> goodsList = goodsMapper.list(name, warehouseId);
        Page<Goods> p = (Page<Goods>) goodsList;

        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());

        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        goodsMapper.delete(ids);
    }

    @Override
    public void save(Goods goods) {
        goods.setCreateTime(LocalDateTime.now());
        goods.setUpdateTime(LocalDateTime.now());
        goodsMapper.insert(goods);
    }

    @Override
    public Goods getById(Integer id) {
        return goodsMapper.getById(id);
    }

    @Override
    public void update(Goods goods) {
        goods.setUpdateTime(LocalDateTime.now());

        goodsMapper.update(goods);
    }
}
